package com.embrace.CookingForMyWife.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.embrace.CookingForMyWife.commom.R;
import com.embrace.CookingForMyWife.entity.OrderedDishes;
import com.embrace.CookingForMyWife.service.OrderedDishesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/api/order")
public class OrderedDishesController {
    @Autowired
    private OrderedDishesService orderedDishesService;

    // 点菜
    @PostMapping("/add")
    public R<OrderedDishes> addDish(@RequestBody OrderedDishes order) {
        order.setCreateTime(LocalDateTime.now());
        orderedDishesService.save(order);
        return R.success(order);
    }

    // 查询某用户点的所有菜
    @GetMapping("/list")
    public R<List<OrderedDishes>> list(@RequestParam Long userId) {
        QueryWrapper<OrderedDishes> query = new QueryWrapper<>();
        query.eq("user_id", userId).orderByDesc("create_time");
        List<OrderedDishes> list = orderedDishesService.list(query);
        return R.success(list);
    }

}
